<template>
  <div :class="className" :style="{ height: height, width: width }" />
</template>

<script>
import * as echarts from "echarts";
require("echarts/theme/macarons"); // echarts theme

export default {
  props: {
    className: {
      type: String,
      default: "chart",
    },
    width: {
      type: String,
      default: "100%",
    },
    height: {
      type: String,
      default: "100%",
    },
    unit: {
      type: String,
      default: "台",
    },
    dataName: {
      type: String,
      default: "台",
    },
    xAxisData: {
      type: Array,
      default: () => []
    },
    seriesData: {
      type: Array,
      default: () => []
    },
    animationDuration: {
      type: Number,
      default: 1000 // 默认动画时长1秒
    },
    animationDelay: {
      type: Number,
      default: 200 // 默认动画延迟200ms
    }
  },
  watch: {
    xAxisData: {
      handler() {
        this.updateChart();
      },
      deep: true
    },
    seriesData: {
      handler() {
        this.updateChart();
      },
      deep: true
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart();
    });
  },
  beforeDestroy() {
    if (!this.chart) {
      return;
    }
    this.chart.dispose();
    this.chart = null;
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, "macarons");
      this.updateChart();
    },
    updateChart() {
      this.chart.setOption({
        // 全局动画配置
        animation: true,
        animationDuration: this.animationDuration,
        animationEasing: 'elasticOut', // 弹性动画效果
        animationDelay: this.animationDelay,
        
        legend: {
          textStyle: {
            color: "#ffffff",
          },
          right: 0,
          itemHeight: 5,
          itemWidth: 20,
          backgroundColor: "#0AA7FF4D",
          // 图例动画
          animation: true,
          animationDurationUpdate: 800
        },
        grid: {
          top: 30,
          left: 40,
          right: 0,
          bottom: 30,
        },
        xAxis: {
          type: "category",
          data: this.xAxisData,
          axisTick: {
            show: false,
          },
          // 坐标轴动画
          animation: true,
          animationDuration: 1000,
          animationEasing: 'cubicInOut'
        },
        yAxis: {
          type: "value",
          splitLine: {
            lineStyle: {
              type: "dashed",
              color: "#B0D7FF33",
            },
          },
          name: `单位：${this.unit}`,
          nameTextStyle: {
            color: "#999999",
          },
          // 坐标轴动画
          animation: true,
          animationDuration: 1000
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'shadow'
          },
          // 提示框动画
          animation: true,
          enterable: true
        },
        series: [
          {
            name: this.dataName,
            data: this.seriesData,
            type: "pictorialBar",
            symbol: "rect",
            symbolRepeat: "fixed",
            symbolMargin: 5,
            symbolClip: true,
            symbolSize: [20, 10],
            itemStyle: {
              color: "#1978E5",
              // 柱状图颜色动画
              color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                { offset: 0, color: '#3BA0FF' }, // 顶部颜色
                { offset: 1, color: '#1978E5' }  // 底部颜色
              ])
            },
            z: 10,
            // 系列动画配置
            animation: true,
            animationDuration: function (idx) {
              // 越往后动画时长越长，形成波浪效果
              return Math.random() * 1000 + 1000;
            },
            animationDelay: function (idx) {
              // 每个柱子有不同延迟，形成依次出现效果
              return idx * 100;
            },
            animationEasing: 'elasticOut',
            // 柱状图上升动画
            showBackground: true,
            backgroundStyle: {
              color: 'rgba(25, 120, 229, 0.2)'
            },
            // 柱状图悬停动画
            emphasis: {
              itemStyle: {
                color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                  { offset: 0, color: '#4BB0FF' },
                  { offset: 1, color: '#2888E5' }
                ]),
                shadowColor: 'rgba(0, 0, 0, 0.5)',
                shadowBlur: 10,
                shadowOffsetY: 5
              }
            }
          }
        ],
        // 全局过渡动画
        graphic: {
          elements: [{
            type: 'text',
            left: 'center',
            top: 'center',
            style: {
              text: '数据加载中...',
              fontSize: 20,
              fontWeight: 'bold',
              fill: '#fff'
            },
            invisible: true // 初始不可见
          }]
        }
      });

      // 添加自定义动画效果
      this.chart.on('finished', () => {
        // 图表渲染完成后可以添加更多动画效果
      });
    }
  },
};
</script>

<style scoped>
.chart {
  transition: all 5s ease;
}
</style>